package com.winmastergames.remotemonitor;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import com.winmastergames.remotemonitor.RefreshTimer2;

import java.util.List;

class WeatherAdapterView extends LinearLayout {    
		public MonitorStatus cStatus;
		public TextView servicesFailControl;
		public TextView temperatureControl;
		protected static final int FAILCOUNTUPDATEMSG = 0x101;
		protected static final int OKCOUNTUPDATEMSG = 0x102;
		public Handler myViewUpdateHandler;
        public WeatherAdapterView(Context context, 
        		MonitorStatus status ) {
            super( context );

            this.setOrientation(HORIZONTAL);
            
            this.cStatus = status;
            
            //========================================================================
            //Add Address TextView
            //========================================================================
            LinearLayout.LayoutParams cityParams = 
                new LinearLayout.LayoutParams(180, LayoutParams.WRAP_CONTENT);
            cityParams.setMargins(1, 1, 1, 1);
 
            TextView cityControl = new TextView( context );
			cityControl.setText( status.getAddress() );
            cityControl.setTextSize(22f);
            cityControl.setTextColor(Color.WHITE);
            addView( cityControl, cityParams);
            
            //========================================================================
            //Add ServicesOk TextView
            //========================================================================
            LinearLayout.LayoutParams temperatureParams = 
                new LinearLayout.LayoutParams(20, LayoutParams.WRAP_CONTENT);
            temperatureParams.setMargins(1, 1, 1, 1);

            temperatureControl = new TextView(context);
            temperatureControl.setText(status.getServicesOk());
            temperatureControl.setTextSize( 14f );
            temperatureControl.setTextColor(Color.GREEN);
            addView( temperatureControl, temperatureParams);  
            
            //========================================================================
            //Add ServicesFail TextView
            //========================================================================
            LinearLayout.LayoutParams servicesFailParams =
            	new LinearLayout.LayoutParams(20, LayoutParams.WRAP_CONTENT);
            servicesFailParams.setMargins(1, 1, 1, 1);
            
            servicesFailControl = new TextView(context);
            servicesFailControl.setText(status.getServicesFail());
            servicesFailControl.setTextSize( 14f );
            servicesFailControl.setTextColor(Color.RED);
            addView( servicesFailControl, servicesFailParams);

            //========================================================================
            //Add Status ImageView
            //========================================================================
            LinearLayout.LayoutParams skyParams = 
                new LinearLayout.LayoutParams(25, LayoutParams.WRAP_CONTENT);
            skyParams.setMargins(5, 5, 5, 5);

			ImageView skyControl = new ImageView( context );
			skyControl.setImageResource( status.getResource() );
			addView( skyControl, skyParams );
			
			//Create Handler
			myViewUpdateHandler = new Handler(){ 
		          // @Override 
		          public void handleMessage(Message msg) { 
		        	  Bundle msgData;
		               switch (msg.what) { 
		                    case FAILCOUNTUPDATEMSG: 
		                         msgData = msg.getData();
		                         servicesFailControl.setText(msgData.getString("setto"));
		                         Log.d("MSG", "Received Fail Services Message: " + msgData.getString("setto"));
		                         break;
		                    case OKCOUNTUPDATEMSG:
		                    	 msgData = msg.getData();
		                    	 temperatureControl.setText(msgData.getString("setto"));
		                    	 Log.d("MSG", "Received Ok Services Message: " + msgData.getString("setto"));
		                    	 break;
		               } 
		               super.handleMessage(msg); 
		          } 
		     };
        }
        public void updateResources()
        {
        	//Update Failed Services TextView.
        	sendMessage(FAILCOUNTUPDATEMSG, cStatus.getServicesFail(), myViewUpdateHandler);
        	sendMessage(OKCOUNTUPDATEMSG, cStatus.getServicesOk(), myViewUpdateHandler);
        }
        public void sendMessage(int msg_what, String msg_setto, Handler viewHandler)
        {
        	String setto = msg_setto;
        	try{
        		Message m = new Message(); 
                m.what = msg_what; 
                Bundle mdata = new Bundle();
                mdata.putString("setto", setto);
                m.setData(mdata);
                viewHandler.sendMessage(m);
        	}
        	catch(Exception e){}
        }
}

public class WeatherAdapter extends BaseAdapter {

    private Context context;
    private CustomAdapterActivity ref;
    private List<MonitorStatus> serverList;

    public WeatherAdapter(CustomAdapterActivity mainRef, Context context, List<MonitorStatus> serverList ) { 
    	this.ref = mainRef;
        this.context = context;
        this.serverList = serverList;
    }

    public int getCount() {                        
        return serverList.size();
    }

    public Object getItem(int position) {     
        return serverList.get(position);
    }

    public long getItemId(int position) {  
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) { 
    	MonitorStatus weather = serverList.get(position);
    	WeatherAdapterView cmview = new WeatherAdapterView(this.context, weather );
    	cmview.updateResources();
		RefreshTimer2 t = new RefreshTimer2(5, cmview, this.ref);
		Log.e("ERR", (String) t.txt_numitems.getText());
        return cmview;
    }

}
